<template>
    <div class="demo">
        <h2>学生姓名:{{name}}</h2>
        <h2>学生年龄:{{age}}</h2>
    </div>
</template>

<script>
import pubsub from 'pubsub-js'
export default {
    name:'Student',
    data() {
        return {
            name:'李宁',
            age:27
        }
    },
    methods:{
        demo(msgName,data){
            console.log('demo',data,this)
            // this.name = e
        }
    },
    mounted(){
        // this.$bus.$on('schoolName',this.demo)
        //订阅一个xxx消息
        /* this.pubId = pubsub.subscribe('xxx', (msgName,val)=> {
            // console.log('有人发布了xxxx消息',msgName,val)
            this.demo(val)
        }) */
        this.pubId = pubsub.subscribe('xxx', this.demo)


    },
    destroyed(){
        //最好在beforeDestroy钩子中，用$off去解绑当前组件所用到的事件
        // this.$bus.$off('schoolName')

        //取消订阅
         pubsub.subscribe(this.pubId )


    }
}
</script>

<style lang="css" scoped>
    .demo{
        background-color:coral;
        padding: 5px;
        margin-top: 5px;
    }
</style>
